package tree;

/**
 * 222. 完全二叉树的节点个数
 *
 * @author Api
 * @date 2023/10/22 14:49
 */
public class Code222_NumberOfNodesOfCompleteBinaryTree {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode() {

        }

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    // 普通(完全)二叉树获取节点个数
    public int countNodes(TreeNode root) {
        if (root == null) {
            return 0;
        }
        // 后序遍历
        int leftNum = countNodes(root.left);
        int right = countNodes(root.right);
        return leftNum + right + 1;
    }
}
